using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using Neo.MetaModel;
using Webbness.Module.VsTool.Interfaces;

namespace Webbness.Module.VsTool
{
    [Serializable]
    public class SingleFileCreator : BaseTemplateCreator, ISingleTemplateCreator
    {
        protected Model _model;
        protected StringDictionary _properties = new StringDictionary();

        #region ISingleTemplateCreator Members

        public void Run()
        {
            string destFileName = Path.Combine(base._outputRoot, base._template.Replace(".vtl", string.Empty));
            if (base._template.EndsWith(".vtl") && !string.IsNullOrEmpty(Path.GetExtension(destFileName)))
            {
                SolutionAndProjectContext ctx = new SolutionAndProjectContext(_model);
                ctx.Extension = Path.GetExtension(destFileName);
                ctx.BaseDirectory = base._outputRoot;
                ctx.ProjectName = base._projectName;
                ctx.UseBuiltins = GetUseBuiltins();
                GenerateOutputFromTemplate(destFileName, base._templatesFolder, base._template, ctx);
                base._createdFiles.Add(destFileName);
            }
        }

        public ArrayList CreatedFiles
        {
            get { return base._createdFiles; }
        }

        public bool CreateFolders
        {
            get { return base._createFolders; }
            set { base._createFolders = value; }
        }

        public bool ForceOverwrite
        {
            get { return base._forceOverwrite; }
            set { base._forceOverwrite = value; }
        }

        public Model Model
        {
            get { return _model; }
            set { _model = value; }
        }

        public string OutputFolder
        {
            get { return base._outputRoot; }
            set { base._outputRoot = value; }
        }

        public string ProjectName
        {
            get { return base._projectName; }
            set { base._projectName = value; }
        }

        public StringDictionary Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        public string Template
        {
            get { return base._template; }
            set { base._template = value; }
        }

        public string TemplatesFolder
        {
            get { return base._templatesFolder; }
            set { base._templatesFolder = value; }
        }

        #endregion

        private bool GetUseBuiltins()
        {
            if (string.IsNullOrEmpty(_properties["UseBuiltins"]))
            {
                return false;
            }
            return Convert.ToBoolean(_properties["UseBuiltins"]);
        }
    }
}